home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tclX-6.4 / help / intro / expressions < prev    next >
Encoding:
Text File  |  1992-12-17  |  11.9 KB  |  256 lines

  1.      EXPRESSIONS
  2.           The second major interpretation applied to strings in Tcl is
  3.           as  expressions.   Several  commands, such as expr, for, and
  4.           if, treat one or more of their arguments as expressions  and
  5.           call    the   Tcl   expression   processors   (Tcl_ExprLong,
  6.           Tcl_ExprBoolean, etc.)  to  evaluate  them.   The  operators
  7.           permitted  in  Tcl expressions are a subset of the operators
  8.           permitted in C expressions, and they have the  same  meaning
  9.           and   precedence   as   the   corresponding   C   operators.
  10.  
  11.           Expressions almost always yield numeric results (integer  or
  12.           floating-point values).  For example, the expression
  13.  
  14.                8.2 + 6
  15.  
  16.           evaluates  to  14.2.   Tcl   expressions   differ   from   C
  17.           expressions  in  the way that operands are specified, and in
  18.           that Tcl expressions support non-numeric operands and string
  19.           comparisons.
  20.  
  21.           A Tcl expression consists  of  a  combination  of  operands,
  22.           operators, and parentheses.  White space may be used between
  23.           the operands and operators and parentheses; it is ignored by
  24.           the  expression  processor.   Where  possible,  operands are
  25.           interpreted  as  integer  values.   Integer  values  may  be
  26.           specified  in  decimal  (the  normal case), in octal (if the
  27.           first character of the operand is 0), or in hexadecimal  (if
  28.           the  first  two  characters  of  the operand are 0x).  If an
  29.           operand does not have  one  of  the  integer  formats  given
  30.           above, then it is treated as a floating-point number if that
  31.           is possible.  Floating-point numbers may be specified in any
  32.           of the ways accepted by an ANSI-compliant C compiler (except
  33.           that the ``f'', ``F'', ``l'', and ``L'' suffixes will not be
  34.           permitted  in  most installations).  For example, all of the
  35.           following are valid floating-point numbers:  2.1,  3.,  6e4,
  36.           7.91e+16.  If no numeric interpretation is possible, then an
  37.           operand is left as a string  (and  only  a  limited  set  of
  38.           operators may be applied to it).
  39.  
  40.           Operators may be specified in any of the following ways:
  41.  
  42.           [1]
  43.                As an numeric value, either integer or floating-point.
  44.  
  45.           [2]
  46.                As  a  Tcl  variable,  using  standard $ notation.  The
  47.                variable's value will be used as the operand.
  48.  
  49.           [3]
  50.                As  a string enclosed in double-quotes.  The expression
  51.                parser will perform backslash,  variable,  and  command
  52.                substitutions  on  the  information between the quotes,
  53.                and use the resulting value as the operand
  54.  
  55.           [4]
  56.                As a string enclosed in braces.  The characters between
  57.                the open brace and matching close brace will be used as
  58.                the operand without any substitutions.
  59.  
  60.           [5]
  61.                As  a  Tcl  command  enclosed in brackets.  The command
  62.                will be executed and its result will  be  used  as  the
  63.                operand.
  64.  
  65.           [6]
  66.                An unquoted string consisting of any number of letters,
  67.                digits, and underscores (but a digit  may  not  be  the
  68.                first character).
  69.  
  70.           Where  substitutions  occur  above   (e.g.   inside   quoted
  71.           strings),  they  are  performed by the expression processor.
  72.           However, an additional layer  of  substitution  may  already
  73.           have  been  performed  by  the  command  parser  before  the
  74.           expression processor was called.  As discussed below, it  is
  75.           usually best to enclose expressions in braces to prevent the
  76.           command  parser  from  performing   substitutions   on   the
  77.           contents.
  78.  
  79.           For  some  examples  of  simple  expressions,  suppose   the
  80.           variable  a has the value 3 and the variable b has the value
  81.           6.  Then the expression on the left  side  of  each  of  the
  82.           lines  below will evaluate to the value on the right side of
  83.           the line:
  84.  
  85.                3.1 + $a                6.1
  86.                2 + "$a.$b"             5.6
  87.                4*[length "6 2"]        8
  88.                {word one} < "word $a"  0
  89.  
  90.  
  91.           The valid operators are listed below, grouped in  decreasing
  92.           order of precedence:
  93.  
  94.           -  ~  !
  95.                               Unary  minus, bit-wise NOT, logical NOT.
  96.                               None of these operands may be applied to
  97.                               string operands, and bit-wise NOT may be
  98.                               applied only to integers.
  99.  
  100.           *  /  %
  101.                               Multiply,  divide,  remainder.   None of
  102.                               these operands may be applied to  string
  103.                               operands,  and  remainder may be applied
  104.                               only to integers.
  105.  
  106.           +  -
  107.                               Add and subtract.  Valid for any numeric
  108.                               operands.
  109.  
  110.           <<  >>
  111.                               Left and right shift.  Valid for integer
  112.                               operands only.
  113.  
  114.           <  >  <=  >=
  115.                               Boolean  less,  greater,  less  than  or
  116.                               equal, and greater than or equal.   Each
  117.                               operator  produces 1 if the condition is
  118.                               true, 0 otherwise.  These operators  may
  119.                               be applied to strings as well as numeric
  120.                               operands,   in   which    case    string
  121.                               comparison is used.
  122.  
  123.           ==  !=
  124.                               Boolean   equal  and  not  equal.   Each
  125.                               operator  produces  a  zero/one  result.
  126.                               Valid for all operand types.
  127.  
  128.           &
  129.                               Bit-wise   AND.    Valid   for   integer
  130.                               operands only.
  131.  
  132.           ^
  133.                               Bit-wise   exclusive   OR.    Valid  for
  134.                               integer operands only.
  135.  
  136.           |
  137.                               Bit-wise OR.  Valid for integer operands
  138.                               only.
  139.  
  140.           &&
  141.                               Logical  AND.   Produces  a  1 result if
  142.                               both operands are non-zero, 0 otherwise.
  143.                               Valid    for   numeric   operands   only
  144.                               (integers or floating-point).
  145.  
  146.           ||
  147.                               Logical OR.  Produces a 0 result if both
  148.                               operands are zero, 1  otherwise.   Valid
  149.                               for  numeric  operands only (integers or
  150.                               floating-point).
  151.  
  152.           x?y:z
  153.                               If-then-else,  as  in C.  If x evaluates
  154.                               to non-zero,  then  the  result  is  the
  155.                               value of y.  Otherwise the result is the
  156.                               value of z.  The x operand must  have  a
  157.                               numeric value.
  158.  
  159.           See the C manual for more details on the results produced by
  160.           each  operator.   All of the binary operators group left-to-
  161.           right within the same precedence level.   For  example,  the
  162.           expression
  163.  
  164.                4*2 < 7
  165.  
  166.           evaluates to 0.
  167.  
  168.           The &&, ||, and ?: operators have ``lazy evaluation'',  just
  169.           as in C, which means that operands are not evaluated if they
  170.           are not needed to determine the outcome.  For example, in
  171.  
  172.                $v ? [a] : [b]
  173.  
  174.           only one of [a] or [b] will actually be evaluated, depending
  175.           on the value of $v.
  176.  
  177.           All internal computations involving integers are  done  with
  178.           the  C  type  long,  and all internal computations involving
  179.           floating-point are  done  with  the  C  type  double.   When
  180.           converting  a string to floating-point, exponent overflow is
  181.           detected and results in a  Tcl  error.   For  conversion  to
  182.           integer  from  string,  detection of overflow depends on the
  183.           behavior of some routines in the  local  C  library,  so  it
  184.           should be regarded as unreliable.  In any case, overflow and
  185.           underflow  are   generally   not   detected   reliably   for
  186.           intermediate results.
  187.  
  188.           Conversion  among  internal  representations  for   integer,
  189.           floating-point, and string operands is done automatically as
  190.           needed.  For  arithmetic  computations,  integers  are  used
  191.           until  some floating-point number is introduced, after which
  192.           floating-point is used.  For example,
  193.  
  194.                5 / 4
  195.           yields the result 1, while
  196.  
  197.                5 / 4.0
  198.                5 / ( [length "abcd" chars] + 0.0 )
  199.  
  200.           both yield the result 1.25.
  201.  
  202.           String values may be used  as  operands  of  the  comparison
  203.           operators,  although  the  expression  evaluator tries to do
  204.           comparisons as integer or floating-point when  it  can.   If
  205.           one  of  the  operands  of  a comparison is a string and the
  206.           other has a numeric value, the numeric operand is  converted
  207.           back to a string using the C sprintf format specifier %d for
  208.           integers and %g for floating-point values.  For example, the
  209.           expressions
  210.  
  211.                "0x03" > "2"
  212.                "0y" < "0x12"
  213.           both evaluate to 1.  The  first  comparison  is  done  using
  214.           integer  comparison,  and  the  second  is done using string
  215.           comparison after the second  operand  is  converted  to  the
  216.           string ``18''.
  217.  
  218.           In general it is safest to enclose an expression  in  braces
  219.           when entering it in a command:  otherwise, if the expression
  220.           contains any white space then the Tcl interpreter will split
  221.           it among several arguments.  For example, the command
  222.  
  223.                expr $a + $b
  224.  
  225.           results in three arguments being passed to expr:  $a, +, and
  226.           $b.  In addition, if the expression isn't in braces then the
  227.           Tcl  interpreter   will   perform   variable   and   command
  228.           substitution  immediately  (it  will  happen  in the command
  229.           parser rather than in the expression parser).  In many cases
  230.           the  expression  is  being  passed  to  a  command that will
  231.           evaluate the expression later (or even many  times  if,  for
  232.           example, the expression is to be used to decide when to exit
  233.           a loop).  Usually the desired goal is to re-do the  variable
  234.           or   command  substitutions  each  time  the  expression  is
  235.           evaluated, rather than once and for all  at  the  beginning.
  236.           For example, the command
  237.  
  238.                for {set i 1} $i<=10 {incr i} {...}*** WRONG ***
  239.  
  240.           is probably intended to iterate over all values of i from  1
  241.           to  10.   After  each iteration of the body of the loop, for
  242.           will pass its second argument to the expression evaluator to
  243.           see  whether  or not to continue processing.  Unfortunately,
  244.           in this case the value of i in the second argument  will  be
  245.           substituted once and for all when the for command is parsed.
  246.           If i was 0 before the for command  was  invoked  then  for's
  247.           second  argument will be 0<=10 which will always evaluate to
  248.           1, even though i's value eventually becomes greater than 10.
  249.           In  the  above case the loop will never terminate.  Instead,
  250.           the expression should be placed in braces:
  251.  
  252.                for {set i 1} {$i<=10} {incr i} {...}*** RIGHT ***
  253.           This causes the substitution of i's value to be delayed;  it
  254.           will be re-done each time the expression is evaluated, which
  255.           is the desired result.
  256.